<!-- SetPassword.vue -->
<template>
    <div class="set-password-container">
      <div class="set-password-box">
        <div class="header">
          <img src="@/assets/images/logo.png" alt="Logo" class="logo" />
          <h2>设置密码</h2>
        </div>
        <el-form ref="setPasswordForm" :model="form" :rules="rules" @keyup.enter="handleSetPassword">
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="form.phone" disabled prefix-icon="Phone">
              <template #prepend>
                <span>+86</span>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="password">
            <el-input v-model="form.password" type="password" placeholder="请输入新密码" />
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input v-model="form.confirmPassword" type="password" placeholder="请再次输入密码" />
          </el-form-item>
          <el-button type="primary" class="submit-button" :loading="loading" @click="handleSetPassword">
            确认设置
          </el-button>
        </el-form>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, reactive } from 'vue'
  import { useRouter } from 'vue-router'
  import { useUserStore } from '@/stores/user';
  import { setPasswordApi } from '@/api/auth'
  import { ElMessage } from 'element-plus'
  
  const router = useRouter()
  const setPasswordForm = ref(null)
  const loading = ref(false)
  const userStore = useUserStore();
  const form = reactive({
    phone: userStore.userInfo || '', // 从登录页面传递的手机号
    password: '',
    confirmPassword: ''
  })
  
  const rules = {
    password: [
      { required: true, message: '请输入新密码', trigger: 'blur' },
      { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
    ],
    confirmPassword: [
      { required: true, message: '请再次输入密码', trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          if (value !== form.password) {
            callback(new Error('两次输入的密码不一致'))
          } else {
            callback()
          }
        },
        trigger: 'blur'
      }
    ]
  }

const handleSetPassword = async () => {
  try {
    await setPasswordForm.value.validate()
    loading.value = true
    const response = await setPasswordApi({
      phone: form.phone,
      password: form.password
    })
    
    if (response.data.code === 201) {
      ElMessage.error(response.data.data)
      return
    }
    
    ElMessage.success('密码设置成功，请重新登录')
    userStore.logout() // 清除可能的临时状态
    router.push('/') // 回到登录页
  } catch (error) {
    ElMessage.error('设置密码失败，请重试')
  } finally {
    loading.value = false
  }
}
  </script>
  
  <style lang="scss" scoped>
  .set-password-container {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f5f7fa;
  }
  
  .set-password-box {
    width: 100%;
    max-width: 400px;
    background: #fff;
    border-radius: 8px;
    padding: 30px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  }
  
  .header {
    text-align: center;
    margin-bottom: 30px;
  }
  
  .logo {
    width: 60px;
    height: 60px;
  }
  
  .header h2 {
    font-size: 24px;
    color: #333;
    margin: 10px 0 0;
  }
  
  .submit-button {
    width: 100%;
    height: 44px;
    font-size: 16px;
  }
  </style>